package util;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.sam.widget.R;

public class SegmentedBar extends SegmentedGroup {
    private int segmentNameSize;
    private int segmentItemWidth;
    private int segmentItemHeight;
    private int segmentCheckedPostion = -1;

    public SegmentedBar(Context context) {
        this(context,null);
    }

    public SegmentedBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        getAttrs(attrs);
        init();
    }

    protected void getAttrs(AttributeSet attrs){
        TypedArray ta = getContext().obtainStyledAttributes(attrs, R.styleable.SegmentedBar);
        try {
            segmentItemWidth = ta.getDimensionPixelSize(R.styleable.SegmentedBar_segmentItemWidth,getResources().getDimensionPixelSize(R.dimen.default_segment_item_width));
            segmentItemHeight = ta.getDimensionPixelSize(R.styleable.SegmentedBar_segmentItemHeight,getResources().getDimensionPixelSize(R.dimen.default_segment_item_height));
            segmentNameSize = ta.getDimensionPixelSize(R.styleable.SegmentedBar_segmentNameSize,getResources().getDimensionPixelSize(R.dimen.default_segment_name_size));
            segmentCheckedPostion = ta.getInt(R.styleable.SegmentedBar_defaultCheckedPostion,segmentCheckedPostion);
        } finally {
            ta.recycle();
        }
    }

    private void init() {
        if(isInEditMode()){
            setSegmentNames(new String[]{getResources().getString(R.string.default_first_segment_name),
                    getResources().getString(R.string.default_second_segment_name),
                    getResources().getString(R.string.default_third_segment_name)});
        }
    }

    /** 对TextView设置不同状态时其文字颜色。 */
//    private ColorStateList createColorStateList(int normal, int checked) {
//        int[] colors = new int[] { checked, checked, checked, normal };
//        int[][] states = new int[4][];
//        states[0] = new int[] { android.R.attr.state_pressed};
//        states[1] = new int[] { android.R.attr.state_selected};
//        states[2] = new int[] { android.R.attr.state_checked };
//        states[3] = new int[] {};
//        ColorStateList colorList = new ColorStateList(states, colors);
//        return colorList;
//    }

    public void setSegmentNames(String[] segmentNames){
        removeAllViews();
        if(segmentNames != null && segmentNames.length > 0){
            for(int i = 0; i < segmentNames.length;i++){
                RadioButton segment = new RadioButton(getContext());
//                segment.setTextColor(createColorStateList(getUnCheckedTintColor(),getTintColor()));
                if(segmentNameSize != 0) segment.setTextSize(segmentNameSize);
                segment.setText(segmentNames[i]);
                segment.setGravity(Gravity.CENTER);
                segment.setButtonDrawable(new ColorDrawable(Color.TRANSPARENT));
                LayoutParams lp = new LayoutParams(segmentItemWidth,segmentItemHeight);
                segment.setLayoutParams(lp);
                segment.setId(i + 100);
                addView(segment);
            }
        }
        updateBackground();

        setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if(onSegmentCheckedChangeListener != null){
                    onSegmentCheckedChangeListener.onSegmentCheckedChanged(SegmentedBar.this,checkedId - 100, (RadioButton) findViewById(checkedId));
                }
            }
        });
    }

    public void setSegmentInfo(String[] segmentNames,int defaultCheckedPos,OnSegmentCheckedChangeListener onSegmentCheckedChangeListener){
        this.onSegmentCheckedChangeListener = onSegmentCheckedChangeListener;
        setSegmentNames(segmentNames);
        setDefaultCheckedItem(defaultCheckedPos);
    }

    private OnSegmentCheckedChangeListener onSegmentCheckedChangeListener;
    public interface OnSegmentCheckedChangeListener{
        void onSegmentCheckedChanged(SegmentedBar segmentedBar, int position, RadioButton segmentButton);
    }

    public int getSegmentNameSize() {
        return segmentNameSize;
    }

    public void setSegmentNameSize(int segmentNameSize) {
        this.segmentNameSize = segmentNameSize;
        updateBackground();
    }

    public int getSegmentItemWidth() {
        return segmentItemWidth;
    }

    public static final int DEFAULT_SEGMENT_ITEM_WIDTH = 0;
    public void setSegmentItemWidth(int segmentItemWidth) {
        if(segmentItemWidth == DEFAULT_SEGMENT_ITEM_WIDTH) {
            this.segmentItemWidth = getResources().getDimensionPixelSize(R.dimen.default_segment_item_width);
        } else {
            this.segmentItemWidth = segmentItemWidth;
        }
        updateBackground();
    }

    public int getSegmentItemHeight() {
        return segmentItemHeight;
    }

    /**
     * 设置默认选中的SegmentItem。必须在调用了setSegmentNames或者setSegmentInfo方法来添加了SegmentItem之后才能调用此方法。
     * @param position
     */
    public void setDefaultCheckedItem(int position){
        if(getChildCount() == 0) throw new NullPointerException("必须在调用了setSegmentNames或者setSegmentInfo方法来添加了SegmentItem之后才能调用此方法。");
        if(position < 0 || position >= getChildCount() || segmentCheckedPostion == position) return;
        ((RadioButton)getChildAt(position)).setChecked(true);
        segmentCheckedPostion = position;
    }

    public static final int DEFAULT_SEGMENT_ITEM_HEIGHT = 0;
    public void setSegmentItemHeight(int segmentItemHeight) {
        if(segmentItemHeight == DEFAULT_SEGMENT_ITEM_HEIGHT) {
            this.segmentItemHeight = getResources().getDimensionPixelSize(R.dimen.default_segment_item_height);
        } else {
            this.segmentItemHeight = segmentItemHeight;
        }
        updateBackground();
    }
}
